package org.pratik.ghine;
import ClassesInfo.configuration;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CardiologistFormServlet
 */
@WebServlet(description = "Forum for Cardiologist", urlPatterns = { "/CardiologistFormServlet" })
public class CardiologistFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Statement st=null;
	private ResultSet rs=null;	
	private RequestDispatcher addTopicJsp,indexDispatcher;
	private configuration conf;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CardiologistFormServlet() {
        super();
        System.out.println("aaaaaaaaaaa");
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub
		conf=new configuration();
		System.out.println("hi from cardio");
		java.util.Date dt=new java.util.Date();
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String currentTime=sdf.format(dt);
		String topic=request.getParameter("topic");
		String detail=request.getParameter("detail");
		String name=request.getParameter("name");
		String email=request.getParameter("email");
		Date now=new Date();
		System.out.println("email recieved "+email);
	try
	{
		
			Class.forName(conf.getDriver()).newInstance();				
			Connection con=null;		
			con =conf.getConnection();
			System.out.println(con);
			st=con.createStatement();
			int result=st.executeUpdate("Insert into forum_question(topic,detail,name,datetime) VALUES("+"'"+topic+"','"+detail+"','"+name+"',"+"'"+currentTime+"'"+")");
			con.close();
			System.out.println(result);
			if(result==1)
			{
				addTopicJsp.forward(request, response);
				return;				
			}
	}// end of try
			catch(Exception e)
			{
				e.printStackTrace();
			}
		
		
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		ServletContext context = config.getServletContext();
		//formJsp = context.getRequestDispatcher("/form.jsp");
		addTopicJsp = context.getRequestDispatcher("/add_topic.jsp");
		indexDispatcher=context.getRequestDispatcher("/index.html");
	}

}
